1、数组长度值等于最后一项的索引值加1.
2、join()方法:使用不同的分隔符来构建这个字符串。只接收一个参数,即可用作分隔符的字符串,然后返回包含所有数组项的字符串。
var colors = [“red”, “green”, “blue”];
alert(colors.join(“,”)); //red, green, blue
alert(colors.join(“||”)); //red|| green||blue
3、队列方法:
添加:unshift(前)<-->push(后)
移除:pop(前)<-->shift(后)
使用时,shift与push一起使用,表示在数组后端添加项,数组前端移除项;
pop与unshift一起使用,方向相反。-->-->
4、重排序方法:reverse()和sort()
比较函数接收两个参数,如果第一个参数应该位于第二个之前则返回一个负数,如果第一个参数应该位于第二个之后则返回一个正数,如果两个参数相等则返回0。
a.sort(compare);
从小到大排序:
function compare(a,b)
{
return a-b;
}
从大到小排序:
function compare(a,b)
{
return b-a;
}
5、操作方法
(1) concat() 方法
创建新数组
var colors = [“red”, “green”, “blue”];
var colors2 = colors.concat(“yellow”, [“black”, “brown”]);
alert(colors); //red, green, blue
alert(colors2); //red, green, blue, yellow, black, brown
(2) slice() 方法
var colors = [“red”, “green”, “blue”, “yellow”, “purple”];
var colors2 = colors.slice(1);
var colors2 = colors.slice(1, 4);
//如果有两个参数,该方法返回起始和结束位置之前的项(但不包括结束位置的项)
alert(colors2); // green, blue, yellow, purple
alert(colors3); // green, blue, yellow
(3) splice() 方法
var colors = [“red”, “green”, “blue”];
var removed = colors.splice(0, 1);
alert(colors); //green, blue
alert(removed); //red,返回的数组中只包含一项
removed = colors.splice(1, 0, “yellow”, “orange”);
alert(colors); //green, yellow, orange, blue
alert(removed); //返回的是一个空数组
removed = colors.splice(1, 1, “red”, “purple”);
alert(colors); //green, red, purple, orange, blue
alert(removed); //yellow,返回的数组中只包含一项
6、位置方法
(1) indexOf() 和 lastIndexOf()
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
alert(numbers.indexOf(4)); // 3
alert(numbers.lastIndexOf(4)); // 5
alert(numbers.indexOf(4, 4)); // 5
alert(numbers.lastIndexOf(4, 4)); // 3
var person = {name: “Nicholas”};
var people = [{name: “Nicholas”}];
var morePeople = [person];
alert(people.indexOf(person)); // -1
alert(morePeople.indexOf(person)); // 0
使用indexOf()和lastIndexOf()方法查找特定项在数组中的位置非常简单。
7、迭代方法
every(), filter(), foreach(), map(), some()
function(item, index, array)
8、归并方法
reduce(), reduceRight()
function(pre, cur, index, array)